﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Configuration;

/// <summary>
///  模組: Menu Module
///  作者: Adams <adamschao@gmail.com>
///  修改: 2013-3-25
///  目的: 專門用來處理自訂選單TreeView模組
///  參數: MENU:TreeView物件
/// </summary>
public class MenuUtil
{
  Menu WebMenu;
  MISEntities entities;
  IOrderedQueryable<MIS_FUNCTION_AP> functions;
  string[] authorizors;

  public MenuUtil(Menu menu)
  {
    entities = new MISEntities();
    menu.Items.Clear();
    WebMenu = menu;

    //取得使用者可存取的FunctionID
   
    authorizors = ((from fun in entities.MIS_FUNCTION_AP
                    where fun.IS_DEFULT_AUTH == "Y"
                    select fun.FUNCTION_ID)
                  .Union(from auth in entities.MIS_AUTHORIZOR_AP
                         where auth.USER_ID == HttpContext.Current.User.Identity.Name
                         select auth.FUNCTION_ID))
                  .Distinct().ToArray();

    //authorizors = entities.MIS_AUTHORIZOR_AP
    //    .Where(a => a.USER_ID == HttpContext.Current.User.Identity.Name)
    //    .Select(a => a.FUNCTION_ID).Distinct().ToArray();
  }

  public void BuildMenu(MenuItem parent_node)
  {
    //判斷是根節點或是子節點
    string condition = parent_node == null ? "999999-999999-99" : parent_node.Value;

    functions = entities.MIS_FUNCTION_AP
          .Where(f => f.FUNCTION_PARENT_ID.Trim() == condition)
          .OrderBy(f => f.FUNCTION_SORT_ORDER);

    foreach (var function in functions)
    {
      MenuItem item = new MenuItem();
      item.Value = function.FUNCTION_ID;
      item.Text = function.FUNCTION_NAME;
      //如果是根節點就使用WebMenu來新增節點
      if (parent_node == null)
      {
        WebMenu.Items.Add(item);
      }
      else
      {
        parent_node.ChildItems.Add(item);
      }
      //如果是資料夾就新增子節點
      if (function.FUNCTION_TYPE.Trim().ToUpper() == "MENU")
      {
        BuildMenu(item);
      }
    }
  }

  public void BuildMenuByUser(MenuItem parent_node)
  {
    //判斷是根節點或是子節點
    string condition = parent_node == null ? "999999-999999-99" : parent_node.Value;

    functions = entities.MIS_FUNCTION_AP
          .Where(f => f.FUNCTION_PARENT_ID.Trim() == condition && authorizors.Contains(f.FUNCTION_ID))
          .OrderBy(f => f.FUNCTION_SORT_ORDER);

    foreach (var function in functions)
    {
      MenuItem item = new MenuItem();
      item.Value = function.FUNCTION_ID;
      item.Text = function.FUNCTION_NAME;
      //item.NavigateUrl = function.FUNCTION_PATH;
      //item.Target = "_blank";      
      if (parent_node == null)
      {
        if (item.Text != "首頁")
        {
          item.NavigateUrl = "javascript:void(0);";
        }
        WebMenu.Items.Add(item);
      }
      else
      {
        parent_node.ChildItems.Add(item);
      }

      if (function.FUNCTION_TYPE.Trim().ToUpper() == "MENU")
      {
        BuildMenuByUser(item);
      }
    }
  }
}

public class TreeViewUtil
{
  TreeView TreeViewMenu;
  MISEntities entities;
  IOrderedQueryable<MIS_FUNCTION_AP> functions;
  string[] authorizors;

  public TreeViewUtil(TreeView treeview)
  {
    entities = new MISEntities();
    TreeViewMenu = treeview;
    TreeViewMenu.Nodes.Clear();

    //取得使用者可存取的FunctionID
    authorizors = entities.MIS_AUTHORIZOR_AP
        .Where(a => a.USER_ID == HttpContext.Current.User.Identity.Name)
        .Select(a => a.FUNCTION_ID).Distinct().ToArray();
  }

  public void BuildTreeView(TreeNode parent_node)
  {
    //判斷是根節點或是子節點
    string condition = parent_node == null ? "999999-999999-99" : parent_node.Value;

    functions = entities.MIS_FUNCTION_AP
          .Where(f => f.FUNCTION_PARENT_ID.Trim() == condition)
          .OrderBy(f => f.FUNCTION_SORT_ORDER);

    foreach (var function in functions)
    {
      TreeNode tn = new TreeNode();
      tn.Value = function.FUNCTION_ID;
      tn.Text = function.FUNCTION_NAME;
      if (parent_node == null)
      {
        TreeViewMenu.Nodes.Add(tn);
      }
      else
      {
        parent_node.ChildNodes.Add(tn);
      }
      //如果是資料夾就新增子節點
      if (function.FUNCTION_TYPE.Trim().ToUpper() == "MENU")
      {
        BuildTreeView(tn);
      }
    }
  }

  public void BuildTreeViewByUser(TreeNode parent_node)
  {
    //判斷是根節點或是子節點
    string condition = parent_node == null ? "999999-999999-99" : parent_node.Value;

    functions = entities.MIS_FUNCTION_AP
          .Where(f => f.FUNCTION_PARENT_ID.Trim() == condition && authorizors.Contains(f.FUNCTION_ID))
          .OrderBy(f => f.FUNCTION_SORT_ORDER);

    foreach (var function in functions)
    {
      TreeNode tn = new TreeNode();
      tn.Value = function.FUNCTION_ID;
      tn.Text = function.FUNCTION_NAME;
      if (parent_node == null)
      {
        TreeViewMenu.Nodes.Add(tn);
      }
      else
      {
        parent_node.ChildNodes.Add(tn);
      }
      //如果是資料夾就新增子節點
      if (function.FUNCTION_TYPE.Trim().ToUpper() == "MENU")
      {
        BuildTreeViewByUser(tn);
      }
    }
  }
}
